package com.lagou.edu.controller;

import com.lagou.common.vo.MsgCode;
import com.lagou.common.vo.User;
import com.lagou.edu.mapper.UserMapper;
import com.lagou.edu.service.CodeServiceClient;
import com.lagou.edu.service.EmailServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class LoginController {
    @Autowired
    CodeServiceClient codeServiceClient;
    @Autowired
    EmailServiceClient emailServiceClient;
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/sendCode/{email}/{password}")
    public String sendCode(@PathVariable String email, @PathVariable String password){
        //调用生成验证码微服务，生成验证码，
        MsgCode resumeOpenState = codeServiceClient.findResumeOpenState(email);
        String registCode = resumeOpenState.getRegistCode();
        //调用邮件微服务发送邮件
        emailServiceClient.sendEmail(email,registCode);
        //将验证码保存在本地
        long l = System.currentTimeMillis();
        userMapper.insertMsgCode(email,registCode,l);
            return "验证码已经发送至邮箱";
    }
    @RequestMapping("/register/{email}/{password}/{registCode}")
    public String register(@PathVariable String email, @PathVariable String password, @PathVariable String registCode){
         //调用验证码微服务判断是否符合条件
        MsgCode msgCode = userMapper.searchMsgCode(email);
        String code = msgCode.getRegistCode();
        if(registCode.equals(code)){
            long l = System.currentTimeMillis() - msgCode.getSystime();
            long minu = l / (1000 * 60);
            if(minu > 5 ){
                return "验证码超时";
            }else{
                User user = userMapper.searchUSER(email);
                if(user != null){
                    return "用户已存在";
                }else{
                    userMapper.insertUSER(email,password);
                    return email+"注册成功";
                }
            }
        }else {
            return "验证码错误";
        }

        //如果符合  在本地服务完成注册功能
        //使用邮箱查询出的验证码是否和传入的验证吗一致，一致  保存密码 和邮箱号
      /*  MsgCode msgCode = loginService.searchMsgCode(email);
        String code = msgCode.getRegistCode();
        if(registCode.equals(code)){
            long l = System.currentTimeMillis() - msgCode.getSystime();
            long minu = l / (1000 * 60);
            if(minu > 5 ){
                return "验证码超时";
            }else{
                User user = loginService.searchUSER(email);
                if(user != null){
                    return "用户已存在";
                }else{
                    loginService.insertUSER(email,password);
                    return email+"注册成功";
                }
            }
        }else {
            return "验证码错误";
        }*/

    }
    @RequestMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password){
      //登录功能移到gatway
        //使用邮箱查询出的验证码是否和传入的验证吗一致，一致  保存密码 和邮箱号
        User user1 = userMapper.searchUSER(email);
        String password1 = user1.getPassword();
        if(password.equals(password1)){
            return "登录成功";
        }else {
            return "账号活密码错误";
        }
    }









//"419716952@qq.com"
    private void sendMsgCodeToEmial(String  email,String  code) {
      /*  //生成6位随机数
        System.out.println((int)((Math.random()*9+1)*100000));
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        // 设置邮件主题
        message.setSubject("注册验证码");
        // 设置邮件发送者，这个跟application.yml中设置的要一致
        message.setFrom(email);
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开，以下类似
        // message.setTo("10*****16@qq.com","12****32*qq.com");
        message.setTo(email);
        // 设置邮件抄送人，可以有多个抄送人
        //message.setCc("12****32*qq.com");
        // 设置隐秘抄送人，可以有多个
        //message.setBcc("7******9@qq.com");
        // 设置邮件发送日期
        message.setSentDate(new Date());
        // 设置邮件的正文
        message.setText("注册验证码为:"+code+"(5分钟有效)");
        // 发送邮件
        javaMailSender.send(message);*/
    }

}
